
var oMainUl = document.getElementsByClassName("mainUl")[0];
var oMinute = document.getElementsByClassName("jishi-minute")[0];
var oSecond = document.getElementsByClassName("jishi-second")[0];
var oThunderOver = document.getElementById("thunderOver");
var thunderNum;
var thunderOver; 
/*游戏开始*/
startGame();
function startGame() {

    var sign = [];
    for (var i = 0; i < 16; i++) {
        for (var j = 0; j < 18; j++) {
            var checkerboardLi = document.createElement("li");
            checkerboardLi.classList.add("initLi");
            checkerboardLi.setAttribute("id", i + "-" + j);
            oMainUl.appendChild(checkerboardLi);
            sign.push({ signValue: 0 });

        }
    }

    thunderShow(sign);
    doingGame(); //游戏开始后调用doingGame()
}

/*棋盘生成*/
// function checkerboard() {

// }



/*游戏进行中*/
function doingGame() {
    /*计时器*/
    // function addTime() {

    // } 

    oMainUl.oncontextmenu = function () {
        return false;
    }
    /*事件委托实现li左键点击事件*/
    oMainUl.addEventListener("mousedown", function (e) {
        var event = e || window.event;
        var oCheckerboardLi = event.target;
        if (event.which === 1) {
            leftClick(oCheckerboardLi);
        } else if (event.which === 3) {
            rightClick(oCheckerboardLi);
        }


    }, false)


    /*左键事件*/
    function leftClick(eventTarget) {
        var oIsLei = document.getElementsByClassName("isLei");
        if (eventTarget && eventTarget.classList.contains("isLei")) {
            stopGame(oIsLei);
        } else {
            spread(eventTarget, leftClick);
        }
    }




    /*右键插旗*/
    function rightClick(eventTarget) {
        if (eventTarget.classList.contains("numShow")) {
            return;
        }
        eventTarget.classList.toggle("flag");
        if (eventTarget.classList.contains("flag") && eventTarget.classList.contains("isLei")) {
            thunderOver--;
            oThunderOver.innerHTML = thunderOver;
        }
        if (!eventTarget.classList.contains("flag") && eventTarget.classList.contains("isLei")){
            thunderOver++;
            oThunderOver.innerHTML = thunderOver;
        }
        if(thunderOver == 0) {
            alert("you success!")
        }
    }


}

/*游戏结束*/
function stopGame(endTerm) {
    console.log("game over")
    for (var i = 0; i < endTerm.length; i++) {
        endTerm[i].classList.add("leiShow");
    }
}


/*数字显示与扩散算法*/
function spread(eventTarget, callback) {
    var n = 0;
    var posArr = event && eventTarget.getAttribute("id").split("-");
    var posX = posArr && +posArr[0];
    var posY = posArr && +posArr[1];
    eventTarget && eventTarget.classList.add("numShow");
    for (var i = posX - 1; i <= posX + 1; i++) {
        for (var j = posY - 1; j <= posY + 1; j++) {
            var agroundLi = document.getElementById(i + '-' + j);
            if (agroundLi && agroundLi.classList.contains("isLei")) {
                n++;
            }
        }
    }

    eventTarget && (eventTarget.innerHTML = n);

    // 以一个方框为中心周围8个格的位置坐标表示矩阵
    // i-1,j-1  i-1,j  i-1,j+1
    // i,j-1    i,j    i,j+1
    // i+1,j-1  i+1,j  i+1,j+1 

    /*扩散算法*/
    if (n == 0) {
        for (var i = posX - 1; i <= posX + 1; i++) {
            for (var j = posY - 1; j <= posY + 1; j++) {
                var nearLi = document.getElementById(i + '-' + j);
                if (nearLi && nearLi.length != 0) {
                    if (!nearLi.classList.contains("check")) {
                        nearLi.classList.add("check");
                        callback(nearLi); //递归
                    }
                }
            }
        }
    }
}


/*雷*/
function thunderShow(sign) {
    thunderNum = 10;
    thunderOver = 10;
    var oInitLi = document.getElementsByClassName("initLi");

    while (thunderNum) {
        var thunderIndex = Math.floor(Math.random() * 100);
        if (sign[thunderIndex].signValue === 0) {
            sign[thunderIndex].signValue === 1;
            oInitLi[thunderIndex].classList.add("isLei");
            thunderNum--;
        }
    }

    //stopGame()触发游戏结束
}

